// MegaStatic.cpp : implementation file
//

#include "stdafx.h"
#include "MegaStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMegaStatic
#define COLOR_BACK			RGB(188, 188, 201)
#define COLOR_TEXT			RGB(102, 102, 102)

CMegaStatic::CMegaStatic()
{
	m_pIcon = NULL;
	m_crBack = COLOR_BACK;
}

CMegaStatic::~CMegaStatic()
{
	if(m_pIcon) 
		delete m_pIcon;
}

void CMegaStatic::SetBitmap(UINT uiIcon)
{
	if(m_pIcon) 
		delete m_pIcon;

	m_pIcon = new CMegaBitmap();
	m_pIcon->LoadBitmap(uiIcon);
}

void CMegaStatic::SetCaption(LPCSTR lpszCaption)
{
	SetWindowText(lpszCaption);
	RedrawWindow();
}

void CMegaStatic::SetBackColor(COLORREF crBack)
{
	m_crBack = crBack;
}

BEGIN_MESSAGE_MAP(CMegaStatic, CStatic)
	//{{AFX_MSG_MAP(CMegaStatic)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMegaStatic message handlers

void CMegaStatic::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CRect rc;
	GetClientRect(&rc);
	
	dc.SetTextColor(COLOR_TEXT);
	dc.SetBkColor(m_crBack);
	dc.FillSolidRect(&rc, m_crBack);

	if(GetStyle() & WS_BORDER)
		dc.DrawEdge(&rc, EDGE_BUMP, BF_RECT);

	if(m_pIcon)
	{
		CSize sz = m_pIcon->GetBitmapDimension();
		m_pIcon->Draw(&dc, 2, rc.top + (rc.Height()-sz.cy)/2);
		rc.left += sz.cx+2;
	}

	/*
	char str[1024];
	GetWindowText(str, 1024);
	*/
	CString strText;
	GetWindowText(strText);
	rc.top += 2;
	rc.bottom -= 2;

	// correct window text in case it doesnt fit window rect
	if(dc.GetTextExtent(strText).cx >= rc.Width())
	{
		CString strNew = strText;
		CSize szText = dc.GetTextExtent(strText);
		int nPos = strText.GetLength();
		while(szText.cx >= rc.Width() && nPos > 0)
		{
			nPos--;
			strNew = strText.Left(nPos)+"...";
			szText = dc.GetTextExtent(strNew);
		}
		strText = strNew;
	}
	dc.DrawText(strText, &rc, DT_LEFT|DT_SINGLELINE|DT_VCENTER);
}
